<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-alert class="ba-table-alert" show-icon>
            <template #default>
                <div class="ba-markdown">
                    <div>
                        1、图文、图片、音频、视频，需要您在微信公众号管理平台进行上传。<a href="https://mp.weixin.qq.com" target="_blank"
                            >微信公众号管理平台</a
                        >
                    </div>
                    <div>2、下方图文列表中，展示的是在微信公众平台已发布的图文信息，非已群发的信息。</div>
                </div>
            </template>
        </el-alert>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('xmwechat.offiaccount.autoreply.type')"
                        type="radio"
                        v-model="baTable.form.items!.type"
                        prop="type"
                        :data="{
                            content: {
                                keyword: t('xmwechat.offiaccount.autoreply.type keyword'),
                                follow: t('xmwechat.offiaccount.autoreply.type follow'),
                                default: t('xmwechat.offiaccount.autoreply.type default'),
                            },
                            childrenAttr: { border: true },
                        }"
                        :placeholder="t('Please select field', { field: t('xmwechat.offiaccount.autoreply.type') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.type == 'keyword'"
                        :label="t('xmwechat.offiaccount.autoreply.keyword')"
                        type="string"
                        v-model="baTable.form.items!.keyword"
                        prop="keyword"
                        :placeholder="t('Please input field', { field: t('xmwechat.offiaccount.autoreply.keyword') })"
                    />
                    <FormItem
                        :label="t('xmwechat.offiaccount.autoreply.msg_type')"
                        type="radio"
                        v-model="baTable.form.items!.msg_type"
                        prop="msg_type"
                        :data="{
                            content: {
                                text: t('xmwechat.offiaccount.autoreply.msg_type text'),
                                news: t('xmwechat.offiaccount.autoreply.msg_type news'),
                                image: t('xmwechat.offiaccount.autoreply.msg_type image'),
                                voice: t('xmwechat.offiaccount.autoreply.msg_type voice'),
                                video: t('xmwechat.offiaccount.autoreply.msg_type video'),
                            },
                        }"
                        :input-attr="{
                            onChange: onChangeMaterialType,
                        }"
                        :placeholder="t('Please select field', { field: t('xmwechat.offiaccount.autoreply.msg_type') })"
                    />

                    <FormItem
                        v-if="baTable.form.items!.msg_type == 'image' || baTable.form.items!.msg_type == 'voice' || baTable.form.items!.msg_type == 'video'"
                        :label="t('xmwechat.offiaccount.autoreply.reply_content')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.reply_content"
                        :key="baTable.form.items!.msg_type"
                        :input-attr="{
                            pk: 'reply_content',
                            field: 'name',
                            'remote-url': '/admin/xmwechat.offiaccount.Material/index',
                            placeholder: '点击选择数据',
                            params: {
                                'type': baTable.form.items!.msg_type,
                            }
                        }"
                    />
                    <FormItem
                        v-else-if="baTable.form.items!.msg_type == 'text'"
                        :label="t('xmwechat.offiaccount.autoreply.reply_content')"
                        type="textarea"
                        v-model="baTable.form.items!.reply_content"
                        prop="reply_content"
                        :placeholder="t('Please input field', { field: t('xmwechat.offiaccount.autoreply.msg_type text') })"
                    />
                    <FormItem
                        v-else-if="baTable.form.items!.msg_type == 'news'"
                        :label="t('xmwechat.offiaccount.autoreply.reply_content')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.reply_content"
                        key="news"
                        :input-attr="{
                            pk: 'reply_content',
                            field: 'title',
                            'remote-url': '/admin/xmwechat.offiaccount.Material/index',
                            placeholder: '点击选择数据',
                            params: {
                                type: 'news',
                            },
                        }"
                    />

                    <FormItem :label="t('xmwechat.offiaccount.autoreply.status')" type="switch" v-model="baTable.form.items!.status" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    type: [buildValidatorData({ name: 'required', title: t('xmwechat.offiaccount.autoreply.type') })],
    msg_type: [buildValidatorData({ name: 'required', title: t('xmwechat.offiaccount.autoreply.msg_type') })],
})

const onChangeMaterialType = () => {
    baTable.form.items!.reply_content = ''
}
</script>

<style scoped lang="scss"></style>
